package ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


import controller.MobilePlanSellAndBillSystem;
import customexceptions.LineAlreadyExistsException;
import customexceptions.LineNotExistsException;
import customexceptions.PlanNotExistsException;
import customexceptions.UserAlreadyExistsException;
import customexceptions.UserNotExistsException;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CreateTitularLineUI extends javax.swing.JFrame {
	private JTextField jTextFieldClientDNI;
	private JLabel jLabelClientDNI;
	private JTextField jTextFieldLineNumber;
	private JLabel jLabelLineNumber;
	private JTextField jTextFieldPlanNumber;
	private JLabel jLabelPlanNumber;
	private JButton jButtonAccept;
	private MobilePlanSellAndBillSystem system;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CreateTitularLineUI inst = new CreateTitularLineUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public CreateTitularLineUI(){
		
	}
	public CreateTitularLineUI(MobilePlanSellAndBillSystem system) {
		super();
		initGUI();
		this.system = system;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jTextFieldClientDNI = new JTextField();
				getContentPane().add(jTextFieldClientDNI);
				jTextFieldClientDNI.setBounds(111, 54, 333, 28);
			}
			{
				jLabelClientDNI = new JLabel();
				getContentPane().add(jLabelClientDNI);
				jLabelClientDNI.setText("Client DNI");
				jLabelClientDNI.setBounds(12, 59, 62, 16);
			}
			{
				jTextFieldLineNumber = new JTextField();
				getContentPane().add(jTextFieldLineNumber);
				jTextFieldLineNumber.setBounds(111, 94, 333, 28);
			}
			{
				jLabelLineNumber = new JLabel();
				getContentPane().add(jLabelLineNumber);
				jLabelLineNumber.setText("Line Number");
				jLabelLineNumber.setBounds(12, 99, 82, 16);
			}	
			{
				jTextFieldPlanNumber = new JTextField();
				getContentPane().add(jTextFieldPlanNumber);
				jTextFieldPlanNumber.setBounds(111, 140, 333, 28);
			}
			{
				jLabelPlanNumber = new JLabel();
				getContentPane().add(jLabelPlanNumber);
				jLabelPlanNumber.setText("Plan Number");
				jLabelPlanNumber.setBounds(8, 152, 85, 16);
			}	
			{
				jButtonAccept = new JButton();
				getContentPane().add(jButtonAccept);
				jButtonAccept.setText("Create");
				jButtonAccept.setBounds(203, 283, 48, 23);

				jButtonAccept.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent evt) 
					{
					
							try {
								system.createTitularLine(jTextFieldClientDNI.getText(), 
										jTextFieldLineNumber.getText());
								system.changeTitularLinePlan(jTextFieldLineNumber.getText(),
										Integer.parseInt(jTextFieldPlanNumber.getText()));
								
							} catch (UserNotExistsException e) {
								ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
								errorReportView.setVisible(true);
							} catch (LineAlreadyExistsException e) {
								ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
								errorReportView.setVisible(true);
							} catch (LineNotExistsException e) {
								ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
								errorReportView.setVisible(true);
							} catch (PlanNotExistsException e) {
								ErrorReportView errorReportView = new ErrorReportView(e.getErrorMessage());
								errorReportView.setVisible(true);
							}

							jTextFieldClientDNI.setText("");
							jTextFieldLineNumber.setText("");
							jTextFieldPlanNumber.setText("");
					}
				});
				
			}//5383217
			/*
					this.dni = dni;
					this.domicilio = domicilio;
					this.email = email;
			 */
			pack();
			this.setSize(510, 417);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
